package com.example.user.inteceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*preHandle() – 在handler执行之前，返回 boolean 值，true 表示继续执行，false 为停止执行并返回。
        postHandle() – 在handler执行之后, 可以在返回之前对返回的结果进行修改
        afterCompletion() – 在请求完全结束后调用，可以用来统计请求耗时等等*/
@Component
public class Cd1_demo implements HandlerInterceptor {
    static {
        System.out.println("interceptor Cd1_demo init");
    }

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("拦截器preHandle在控制器方法执行前执行");
            //true：表示放行
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("拦截器postHandle在控制器方法执行后执行");
        }

        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("拦截器afterCompletion在请求完成后执行");
        }

}
